package com.log530_projetfinal;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class Fragments extends Fragment implements OnSeekBarChangeListener {

	private String name;
	private int id;

	private SeekBar sbNbPlantes;
	private SeekBar sbPointViePlante;
	private SeekBar sbMaturitePlante;
	private SeekBar sbVitalitePlante;
	private SeekBar sbDimensionPlante;
	private SeekBar sbBoisementPlante;

	private TextView nbPlantes;
	private TextView pointViePlante;
	private TextView maturitePlante;
	private TextView vitalitePlante;
	private TextView dimensionPlante;
	private TextView boisementPlante;

	private SeekBar sbNbLapins;
	private SeekBar sbPointVieLapin;
	private SeekBar sbMaturiteLapin;
	private SeekBar sbVitaliteLapin;
	private SeekBar sbDimensionLapin;
	private SeekBar sbVisionLapin;
	private SeekBar sbOdoratLapin;

	private TextView nbLapins;
	private TextView pointVieLapin;
	private TextView maturiteLapin;
	private TextView vitaliteLapin;
	private TextView dimensionLapin;
	private TextView visionLapin;
	private TextView odoratLapin;

	private SeekBar sbNbLoups;
	private SeekBar sbPointVieLoups;
	private SeekBar sbMaturiteLoups;
	private SeekBar sbVitaliteLoups;
	private SeekBar sbDimensionLoups;
	private SeekBar sbVisionLoups;
	private SeekBar sbOdoratLoups;

	private TextView nbLoups;
	private TextView pointVieLoups;
	private TextView maturiteLoups;
	private TextView vitaliteLoups;
	private TextView dimensionLoups;
	private TextView visionLoups;
	private TextView odoratLoups;

	private Map<Integer, TextView> mapPlante = new HashMap<Integer, TextView>();
	private Map<Integer, TextView> mapLapin = new HashMap<Integer, TextView>();
	private Map<Integer, TextView> mapLoup = new HashMap<Integer, TextView>();

	private OnItemSelectedListener listener;

	public Fragments(String name, int id) {
		super();
		this.name = name;
		this.id = id;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		// Inflate the layout for this fragment
		View myInflatedView = inflater.inflate(getResources().getIdentifier("fragment" + id, "layout", container.getContext().getPackageName()), container, false);
		
		SharedPreferences sharedPref = getActivity().getSharedPreferences("TEST",Context.MODE_PRIVATE);
		
		
	
		
		switch (id) {
		case 1:
			sbNbPlantes = (SeekBar) myInflatedView.findViewById(R.id.sbNbPlantes);
			sbNbPlantes.setOnSeekBarChangeListener(this);

			sbPointViePlante = (SeekBar) myInflatedView.findViewById(R.id.sbPointViePlante);
			sbPointViePlante.setOnSeekBarChangeListener(this);

			sbMaturitePlante = (SeekBar) myInflatedView.findViewById(R.id.sbMaturitePlante);
			sbMaturitePlante.setOnSeekBarChangeListener(this);

			sbVitalitePlante = (SeekBar) myInflatedView.findViewById(R.id.sbVitalitePlante);
			sbVitalitePlante.setOnSeekBarChangeListener(this);

			sbDimensionPlante = (SeekBar) myInflatedView.findViewById(R.id.sbDimensionPlante);
			sbDimensionPlante.setOnSeekBarChangeListener(this);

			sbBoisementPlante = (SeekBar) myInflatedView.findViewById(R.id.sbBoisementPlante);
			sbBoisementPlante.setOnSeekBarChangeListener(this);

			nbPlantes = (TextView) myInflatedView.findViewById(R.id.nbPlantes);
			pointViePlante = (TextView) myInflatedView.findViewById(R.id.pointViePlante);
			maturitePlante = (TextView) myInflatedView.findViewById(R.id.maturitePlante);
			vitalitePlante = (TextView) myInflatedView.findViewById(R.id.vitalitePlante);
			dimensionPlante = (TextView) myInflatedView.findViewById(R.id.dimensionPlante);
			boisementPlante = (TextView) myInflatedView.findViewById(R.id.boisementPlante);

			mapPlante.put(1, nbPlantes);
			mapPlante.put(2, pointViePlante);
			mapPlante.put(3, maturitePlante);
			mapPlante.put(4, vitalitePlante);
			mapPlante.put(5, dimensionPlante);
			mapPlante.put(6, boisementPlante);
			
			nbPlantes.setText(String.valueOf(sharedPref.getInt("nbPlantes", 0)));
			sbNbPlantes.setProgress(sharedPref.getInt("nbPlantes", 0));
			
			pointViePlante.setText(String.valueOf(sharedPref.getInt("pointViePlante", 0)));
			sbPointViePlante.setProgress(sharedPref.getInt("pointViePlante", 0));
			
			maturitePlante.setText(String.valueOf(sharedPref.getInt("maturitePlante", 0)));
			sbMaturitePlante.setProgress(sharedPref.getInt("maturitePlante", 0));
			
			vitalitePlante.setText(String.valueOf(sharedPref.getInt("vitalitePlante", 0)));
			sbVitalitePlante.setProgress(sharedPref.getInt("vitalitePlante", 0));
			
			dimensionPlante.setText(String.valueOf(sharedPref.getInt("dimensionPlante", 0)));
			sbDimensionPlante.setProgress(sharedPref.getInt("dimensionPlante", 0));
			
			boisementPlante.setText(String.valueOf(sharedPref.getInt("boisementPlante", 0)));
			sbBoisementPlante.setProgress(sharedPref.getInt("boisementPlante", 0));

			break;
		case 2:

			sbNbLapins = (SeekBar) myInflatedView.findViewById(R.id.sbNbLapins);
			sbNbLapins.setOnSeekBarChangeListener(this);

			sbPointVieLapin = (SeekBar) myInflatedView.findViewById(R.id.sbPointVieLapin);
			sbPointVieLapin.setOnSeekBarChangeListener(this);

			sbMaturiteLapin = (SeekBar) myInflatedView.findViewById(R.id.sbMaturiteLapin);
			sbMaturiteLapin.setOnSeekBarChangeListener(this);

			sbVitaliteLapin = (SeekBar) myInflatedView.findViewById(R.id.sbVitaliteLapin);
			sbVitaliteLapin.setOnSeekBarChangeListener(this);

			sbDimensionLapin = (SeekBar) myInflatedView.findViewById(R.id.sbDimensionLapin);
			sbDimensionLapin.setOnSeekBarChangeListener(this);

			sbVisionLapin = (SeekBar) myInflatedView.findViewById(R.id.sbVisionLapin);
			sbVisionLapin.setOnSeekBarChangeListener(this);

			sbOdoratLapin = (SeekBar) myInflatedView.findViewById(R.id.sbOdoratLapin);
			sbOdoratLapin.setOnSeekBarChangeListener(this);

			nbLapins = (TextView) myInflatedView.findViewById(R.id.nbLapins);
			pointVieLapin = (TextView) myInflatedView.findViewById(R.id.pointVieLapin);
			maturiteLapin = (TextView) myInflatedView.findViewById(R.id.maturiteLapin);
			vitaliteLapin = (TextView) myInflatedView.findViewById(R.id.vitaliteLapin);
			dimensionLapin = (TextView) myInflatedView.findViewById(R.id.dimensionLapin);
			visionLapin = (TextView) myInflatedView.findViewById(R.id.visionLapin);
			odoratLapin = (TextView) myInflatedView.findViewById(R.id.odoratLapin);

			mapLapin.put(1, nbLapins);
			mapLapin.put(2, pointVieLapin);
			mapLapin.put(3, maturiteLapin);
			mapLapin.put(4, vitaliteLapin);
			mapLapin.put(5, dimensionLapin);
			mapLapin.put(6, visionLapin);
			mapLapin.put(7, odoratLapin);
			
			nbLapins.setText(String.valueOf(sharedPref.getInt("nbLapins", 0)));
			sbNbLapins.setProgress(sharedPref.getInt("nbLapins", 0));
			
			pointVieLapin.setText(String.valueOf(sharedPref.getInt("pointVieLapin", 0)));
			sbPointVieLapin.setProgress(sharedPref.getInt("pointVieLapin", 0));
			
			maturiteLapin.setText(String.valueOf(sharedPref.getInt("maturiteLapin", 0)));
			sbMaturiteLapin.setProgress(sharedPref.getInt("maturiteLapin", 0));
			
			vitaliteLapin.setText(String.valueOf(sharedPref.getInt("vitaliteLapin", 0)));
			sbVitaliteLapin.setProgress(sharedPref.getInt("vitaliteLapin", 0));
			
			dimensionLapin.setText(String.valueOf(sharedPref.getInt("dimensionLapin", 0)));
			sbDimensionLapin.setProgress(sharedPref.getInt("dimensionLapin", 0));
			
			visionLapin.setText(String.valueOf(sharedPref.getInt("visionLapin", 0)));
			sbVisionLapin.setProgress(sharedPref.getInt("visionLapin", 0));
			
			
			odoratLapin.setText(String.valueOf(sharedPref.getInt("odoratLapin", 0)));
			sbOdoratLapin.setProgress(sharedPref.getInt("odoratLapin", 0));
			break;
		case 3:
			sbNbLoups = (SeekBar) myInflatedView.findViewById(R.id.sbNbLoups);
			sbNbLoups.setOnSeekBarChangeListener(this);

			sbPointVieLoups = (SeekBar) myInflatedView.findViewById(R.id.sbPointVieLoup);
			sbPointVieLoups.setOnSeekBarChangeListener(this);

			sbMaturiteLoups = (SeekBar) myInflatedView.findViewById(R.id.sbMaturiteLoup);
			sbMaturiteLoups.setOnSeekBarChangeListener(this);

			sbVitaliteLoups = (SeekBar) myInflatedView.findViewById(R.id.sbVitaliteLoup);
			sbVitaliteLoups.setOnSeekBarChangeListener(this);

			sbDimensionLoups = (SeekBar) myInflatedView.findViewById(R.id.sbDimensionLoup);
			sbDimensionLoups.setOnSeekBarChangeListener(this);

			sbVisionLoups = (SeekBar) myInflatedView.findViewById(R.id.sbVisionLoup);
			sbVisionLoups.setOnSeekBarChangeListener(this);

			sbOdoratLoups = (SeekBar) myInflatedView.findViewById(R.id.sbOdoratLoup);
			sbOdoratLoups.setOnSeekBarChangeListener(this);

			nbLoups = (TextView) myInflatedView.findViewById(R.id.nbLoups);
			pointVieLoups = (TextView) myInflatedView.findViewById(R.id.pointVieLoup);
			maturiteLoups = (TextView) myInflatedView.findViewById(R.id.maturiteLoup);
			vitaliteLoups = (TextView) myInflatedView.findViewById(R.id.vitaliteLoup);
			dimensionLoups = (TextView) myInflatedView.findViewById(R.id.dimensionLoup);
			visionLoups = (TextView) myInflatedView.findViewById(R.id.visionLoup);
			odoratLoups = (TextView) myInflatedView.findViewById(R.id.odoratLoup);

			mapLoup.put(1, nbLoups);
			mapLoup.put(2, pointVieLoups);
			mapLoup.put(3, maturiteLoups);
			mapLoup.put(4, vitaliteLoups);
			mapLoup.put(5, dimensionLoups);
			mapLoup.put(6, visionLoups);
			mapLoup.put(7, odoratLoups);
			
			nbLoups.setText(String.valueOf(sharedPref.getInt("nbLoups", 0)));
			sbNbLoups.setProgress(sharedPref.getInt("nbLoups", 0));
			
			pointVieLoups.setText(String.valueOf(sharedPref.getInt("pointVieLoups", 0)));
			sbPointVieLoups.setProgress(sharedPref.getInt("pointVieLoups", 0));
			
			maturiteLoups.setText(String.valueOf(sharedPref.getInt("maturiteLoups", 0)));
			sbMaturiteLoups.setProgress(sharedPref.getInt("maturiteLoups", 0));
			
			vitaliteLoups.setText(String.valueOf(sharedPref.getInt("vitaliteLoups", 0)));
			sbVitaliteLoups.setProgress(sharedPref.getInt("vitaliteLoups", 0));
			
			dimensionLoups.setText(String.valueOf(sharedPref.getInt("dimensionLoups", 0)));
			sbDimensionLoups.setProgress(sharedPref.getInt("dimensionLoups", 0));
			
			visionLoups.setText(String.valueOf(sharedPref.getInt("visionLoups", 0)));
			sbVisionLoups.setProgress(sharedPref.getInt("visionLoups", 0));
			
			odoratLoups.setText(String.valueOf(sharedPref.getInt("odoratLoups", 0)));
			sbOdoratLoups.setProgress(sharedPref.getInt("odoratLoups", 0));
			break;
		}

		return myInflatedView;
	}

	public interface OnItemSelectedListener {
		public void onRssItemSelectedPlantes(Map<Integer, TextView> mapValues);

		public void onRssItemSelectedLapins(Map<Integer, TextView> mapValues);

		public void onRssItemSelectedLoups(Map<Integer, TextView> mapValues);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof OnItemSelectedListener) {
			listener = (OnItemSelectedListener) activity;
		} else {
			throw new ClassCastException(activity.toString() + " must implemenet MyListFragment.OnItemSelectedListener");
		}
	}

	public String getName() {
		return name;
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

		switch (seekBar.getId()) {

		case R.id.sbNbPlantes:
			nbPlantes.setText(String.valueOf(progress));
			break;
		case R.id.sbPointViePlante:
			pointViePlante.setText(String.valueOf(progress));
			break;
		case R.id.sbMaturitePlante:
			maturitePlante.setText(String.valueOf(progress));
			break;
		case R.id.sbVitalitePlante:
			vitalitePlante.setText(String.valueOf(progress));
			break;
		case R.id.sbDimensionPlante:
			dimensionPlante.setText(String.valueOf(progress));
			break;
		case R.id.sbBoisementPlante:
			boisementPlante.setText(String.valueOf(progress));
			break;
		case R.id.sbNbLapins:
			nbLapins.setText(String.valueOf(progress));
			break;
		case R.id.sbPointVieLapin:
			pointVieLapin.setText(String.valueOf(progress));
			break;
		case R.id.sbMaturiteLapin:
			maturiteLapin.setText(String.valueOf(progress));
			break;
		case R.id.sbVitaliteLapin:
			vitaliteLapin.setText(String.valueOf(progress));
			break;
		case R.id.sbDimensionLapin:
			dimensionLapin.setText(String.valueOf(progress));
			break;
		case R.id.sbVisionLapin:
			visionLapin.setText(String.valueOf(progress));
			break;
		case R.id.sbOdoratLapin:
			odoratLapin.setText(String.valueOf(progress));
			break;
		case R.id.sbNbLoups:
			nbLoups.setText(String.valueOf(progress));
			break;
		case R.id.sbPointVieLoup:
			pointVieLoups.setText(String.valueOf(progress));
			break;
		case R.id.sbMaturiteLoup:
			maturiteLoups.setText(String.valueOf(progress));
			break;
		case R.id.sbVitaliteLoup:
			vitaliteLoups.setText(String.valueOf(progress));
			break;
		case R.id.sbDimensionLoup:
			dimensionLoups.setText(String.valueOf(progress));
			break;
		case R.id.sbVisionLoup:
			visionLoups.setText(String.valueOf(progress));
			break;
		case R.id.sbOdoratLoup:
			odoratLoups.setText(String.valueOf(progress));
			break;
		}
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		
		SharedPreferences sharedPref = getActivity().getSharedPreferences("TEST",Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPref.edit();

		switch (seekBar.getId()) {

		case R.id.sbNbPlantes:
			mapPlante.put(1, nbPlantes);
			editor.putInt("nbPlantes", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbPointViePlante:
			mapPlante.put(2, pointViePlante);
			editor.putInt("pointViePlante", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbMaturitePlante:
			mapPlante.put(3, maturitePlante);
			editor.putInt("maturitePlante", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbVitalitePlante:
			mapPlante.put(4, vitalitePlante);
			editor.putInt("vitalitePlante", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbDimensionPlante:
			mapPlante.put(5, dimensionPlante);
			editor.putInt("dimensionPlante", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbBoisementPlante:
			mapPlante.put(6, boisementPlante);
			editor.putInt("boisementPlante", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbNbLapins:
			mapLapin.put(1, nbLapins);
			editor.putInt("nbLapins", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbPointVieLapin:
			mapLapin.put(2, pointVieLapin);
			editor.putInt("pointVieLapin", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbMaturiteLapin:
			mapLapin.put(3, maturiteLapin);
			editor.putInt("maturiteLapin", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbVitaliteLapin:
			mapLapin.put(4, vitaliteLapin);
			editor.putInt("vitaliteLapin", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbDimensionLapin:
			mapLapin.put(5, dimensionLapin);
			editor.putInt("dimensionLapin", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbVisionLapin:
			mapLapin.put(6, visionLapin);
			editor.putInt("visionLapin", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbOdoratLapin:
			mapLapin.put(7, odoratLapin);
			editor.putInt("odoratLapin", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbNbLoups:
			mapLoup.put(1, nbLoups);
			editor.putInt("nbLoups", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbPointVieLoup:
			mapLoup.put(2, pointVieLoups);
			editor.putInt("pointVieLoups", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbMaturiteLoup:
			mapLoup.put(3, maturiteLoups);
			editor.putInt("maturiteLoups", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbVitaliteLoup:
			mapLoup.put(4, vitaliteLoups);
			editor.putInt("vitaliteLoups", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbDimensionLoup:
			mapLoup.put(5, dimensionLoups);
			editor.putInt("dimensionLoups", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbVisionLoup:
			mapLoup.put(6, visionLoups);
			editor.putInt("visionLoups", seekBar.getProgress());
			editor.commit();
			break;
		case R.id.sbOdoratLoup:
			mapLoup.put(7, odoratLoups);
			editor.putInt("odoratLoups", seekBar.getProgress());
			editor.commit();
			break;
		}
		if (mapPlante.size() > 0)
			listener.onRssItemSelectedPlantes(mapPlante);
		if (mapLapin.size() > 0)
			listener.onRssItemSelectedLapins(mapLapin);
		if (mapLoup.size() > 0)
			listener.onRssItemSelectedLoups(mapLoup);
	}

}
